﻿using FictionsDownload.BLL;
using FictionsDownload.DAL;
using FictionsDownload.Events;
using FictionsDownload.Model;
using FictionsDownload.Static;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FictionsDownload.UI
{
    public partial class FormMain : Form
    {
        #region 公共属性-窗体配置
        public Point WindowPoint
        {
            get
            {
                string strSize = AccessAppSettings.Get("WindowPoint");
                if (strSize != null)
                {
                    string[] strArr = strSize.Split(',');
                    Point point = new Point(int.Parse(strArr[0]), int.Parse(strArr[1]));
                    if(point.X<1 || point.X > 1000)
                    {
                        point.X = 1;
                    }
                    if(point.Y<1 || point.Y > 1000)
                    {
                        point.Y = 1;
                    }
                    return point;
                }
                else
                {
                    return new Point(1, 1);
                }
            }
            set
            {
                AccessAppSettings.Update("WindowPoint", value.X + "," + value.Y);
            }
        }
        public Size WindowSize
        {
            get
            {
                string strSize = AccessAppSettings.Get("WindowSize");
                if (strSize != null)
                {
                    string[] strArr = strSize.Split(',');
                    Size size = new Size(int.Parse(strArr[0]), int.Parse(strArr[1]));
                    size.Width = size.Width < 500 ? 500 : size.Width;
                    size.Height = size.Height < 300 ? 300 : size.Height;
                    return size;
                }
                else
                {
                    return new Size(1500, 1200);
                }
            }
            set
            {
                AccessAppSettings.Update("WindowSize", value.Width + "," + value.Height);
            }
        }
        #endregion

        #region 公共属性-窗体布局
        //标题栏高度
        public int TilteBarHeight{get; set;}
        //BookTree的起始Y坐标
        public int BookTreeY { get; set; }

        #endregion

        #region 变量
        public FormTitleBar formTitleBar;
        public FormBookTree formBookTree;
        public FormReadView formReadView;
        public FormDirectorys formDirectorys;
        private BLL.Directory directory = new BLL.Directory();
        #endregion

        #region 构造初始化
        public FormMain()
        {
            InitializeComponent();
            this.Size = WindowSize;
            this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;//在窗体初始化后添加一句代码
            FormTitleBarInit();
            FormBookTreeInit();
            FormReadViewInit();
            FormDirectorysInit();
        }
        private void FormTitleBarInit()
        {
            //pl_TitleBar 的起点是最左边最上面
            this.pl_TitleBar.Location = new Point(0, 0);
            //实例对象
            formTitleBar = FormTitleBar.GetIntance;
            //高度20，宽度和容器等大。
            this.pl_TitleBar.Size = new Size(this.ClientSize.Width, formTitleBar.Size.Height);
            //容器设置
            formTitleBar.Parent = this.pl_TitleBar;
            formTitleBar.Dock = DockStyle.Fill;
            //窗口布局完成后，控件二次调整
            formTitleBar.InitializeComponentCode();
        }
        /// <summary>
        /// FormBookTree初始化
        /// </summary>
        private void FormBookTreeInit()
        {
            this.pl_BookTree.Location = new Point(0,TilteBarHeight);
            //pl_BookTree的底部和容器贴合。
            this.pl_BookTree.Size = new Size(this.pl_BookTree.Size.Width, this.ClientSize.Height-this.pl_BookTree.Location.Y);
            //
            formBookTree = FormBookTree.GetIntance;
            formBookTree.Parent = this.pl_BookTree;
            formBookTree.Dock = DockStyle.Fill;
            //窗口布局完成后，控件二次调整
            formBookTree.InitializeComponentCode();
        }
        /// <summary>
        /// FormReadView初始化
        /// </summary>
        private void FormReadViewInit()
        {
            //pl_ReadView 左边和pl_BookTree的右边贴紧。底部和容器贴合
            this.pl_ReadView.Location = new Point(this.pl_BookTree.Size.Width, TilteBarHeight);
            this.pl_ReadView.Size = new Size(this.pl_ReadView.Size.Width, this.ClientSize.Height - this.pl_ReadView.Location.Y);
            //实例对象
            formReadView = FormReadView.GetIntance;
            //容器设置
            formReadView.Parent = this.pl_ReadView;
            formReadView.Dock = DockStyle.Fill;
            //窗口布局完成后，控件二次调整
            formReadView.InitializeComponentCode();
            formReadView.SelectionDirectory += new EventHandler<SelectionDirectoryEventArgs>(FrmReadViewSelectionDirectory);
            formReadView.NextDirectory += new EventHandler(FrmReadViewNextDirectory);
            formReadView.PrevDirectory += new EventHandler(FrmReadViewPrevDirectory);
        }
        /// <summary>
        /// FormDirectorys初始化
        /// </summary>
        private void FormDirectorysInit()
        {
            //pl_Directorys 左边和pl_ReadView的右边贴紧。底部和容器贴合
            //pl_ReadView的右位置
            int pl_ReadViewRight = this.pl_ReadView.Size.Width + this.pl_BookTree.Size.Width;
            this.pl_Directorys.Location = new Point(pl_ReadViewRight, TilteBarHeight);
            this.pl_Directorys.Size = new Size(this.ClientSize.Width- pl_ReadViewRight, this.ClientSize.Height - this.pl_Directorys.Location.Y);
            //实例对象
            formDirectorys = FormDirectorys.GetIntance;
            //容器设置
            formDirectorys.Parent = this.pl_Directorys;
            formDirectorys.Dock = DockStyle.Fill;
            //窗口布局完成后，控件二次调整
            formDirectorys.InitializeComponentCode();
            //事件
            formDirectorys.SetDirectory += new EventHandler(FrmDirectorysSetDirectory);
            formDirectorys.DownloadDirectory += new EventHandler(FrmDirectorysDownloadDirectory);

        }
        #endregion
        /// <summary>
        /// 弹出FormBookEdit窗口
        /// </summary>
        /// <param name="state"></param>
        /// <param name="id"></param>
        public void ShowFormBook(EditState state,string uuid)
        {
            FormBookEdit formBook = new FormBookEdit(state, uuid);
            formBook.Location = new Point(Location.X+200, Location.Y+70);
            formBook.StartPosition = FormStartPosition.Manual;
            formBook.ShowDialog();
            formBookTree.tv_BooksRefresh();
        }
        /// <summary>
        /// 修改ReadViewBook显示的书籍
        /// </summary>
        public void SetReadViewBook(string bookId)
        {
            formReadView.SetBook(bookId);
            formDirectorys.SetBook(bookId);
        }
        private void FrmReadViewSelectionDirectory(object sender,SelectionDirectoryEventArgs e)
        {
            formDirectorys.SelectionDirectory(e.DirectoryID);
        }
        private void FrmReadViewNextDirectory(object sender, EventArgs e)
        {
            formDirectorys.NextDirectory();
        }
        private void FrmReadViewPrevDirectory(object sender, EventArgs e)
        {
            formDirectorys.PrevDirectory();
        }
        private void FormMain_SizeChanged(object sender, EventArgs e)
        {
            WindowSize = ((FormMain)sender).ClientSize;
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            ((FormMain)sender).Location = WindowPoint;
        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            WindowPoint = ((FormMain)sender).Location;
        }
        private void FrmDirectorysSetDirectory(object sender, EventArgs e)
        {
            formReadView.SetReadText((string)sender);
        }
        private void FrmDirectorysDownloadDirectory(object sender, EventArgs e)
        {
            string id = (string)sender;
            DirectoryModel directoryModel = directory.Get<DirectoryModel>(m=>m.Id == id);
            directoryModel.Len = null;
            directory.Update(directoryModel);
            directory.SaveChanges();
            formBookTree.ThreadDownload.Start(id, new DirectoryModel().GetType());
        }
    }
}
